﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LineStickerDwonload.Linehelper
{
    internal class ExcavateScanner
    {
        /// <summary>
        /// 递归扫描日志记录
        /// </summary>
        public static readonly string LogPath = StaticValue.BinPath + "ScannerLog.txt";

        /// <summary>
        /// 获取扫描列表
        /// </summary>
        public static List<string> RunList = FileHelper.ReadAllLine(LogPath);

        /// <summary>
        /// 自动启动扫描
        /// </summary>
        /// <param name="ScannerLink"></param>
        /// <param name="RecursionCount"></param>
        public void AutoSvae(String ScannerLink, int RecursionCount)
        {
            List<string> AllList = Scanner(ScannerLink, RecursionCount);

            Console.WriteLine("总计结果" + AllList.Count);
            new ExcavateDownload().StartDownload(AllList);
        }

        /// <summary>
        /// 递归扫描，查询更多资源
        /// </summary>
        /// <param name="ScannerLink"></param>
        /// <param name="RecursionCount">递归次数 到0为止</param>
        public List<string> Scanner(String ScannerLink, int RecursionCount)
        {
            string Shtml = WebHelper.HttpPcGet(ScannerLink);
            List<string> ALLList = new List<string>();
            List<string> ScannerList = HtmlHelper.GetLinkVlaueList(Shtml, "//ul/li[@class='mdCMN02Li']/a");
            --RecursionCount;
            Console.WriteLine(ScannerList.Count + "递归" + RecursionCount);
            if (RecursionCount >= 0)
                foreach (var item in ScannerList)
                {
                    string ComicLink = "https://store.line.me" + item;
                    Console.WriteLine(ComicLink);
                    if (ALLList.Contains(ComicLink))
                    {
                        Console.WriteLine("已经存在 不重复添加");
                        continue;
                    }

                    ALLList.Add(ComicLink);
                    RunList.Add(ComicLink);

                    ALLList.AddRange(Scanner(ComicLink, RecursionCount));
                    Console.WriteLine($"递归次数{RecursionCount}, 得到数据{ScannerList.Count},总计{ALLList.Count}");
                    FileHelper.AppendText(LogPath, ComicLink);
                }
            Console.WriteLine("返回数据" + ALLList.Count);
            return ALLList;
        }
    }
}